﻿
//Timer, the timer class.
LightScript.using("LightScript.Core", "LightScript.Components.Component", function()
{
    LightScript.Core.createNamespace("LightScript.Components");
    var thisClass = LightScript.Core.createClass("LightScript.Components.Timer: LightScript.Components.Component", 
    function(callback, interval, times)
    {
        LightScript.Core.base(this, LightScript.Components.Component, [false]);

        this.callback = callback;
        this.interval = interval;
        this.times = times || 0;
        this._executedTimes = 0;
        this.start();
    });

    //# Private Members
    thisClass.prototype._executedTimes = null;
    thisClass.prototype._hwnd = null;

    //# Constructions

    //# Public Members
    thisClass.prototype.callback = null;
    thisClass.prototype.interval = null;
    thisClass.prototype.times = null;

    //# Private Methods

    //# Public Methods
    thisClass.prototype.start = function()
    {
        if(this.times === 1)
            setTimeout(this.callback, this.interval);
        else
        {
            var me = this;
            this._hwnd = setInterval(function()
            {
                me.callback();
                if(me.times !=0 && ++me._executedTimes == me.times)
                {
                    clearInterval(me._hwnd);
                }
            }, me.interval);
        }
    }

    //# Shortcuts
});
